package com.guzhen.wallpager;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import com.airbnb.lottie.LottieComposition;
import com.airbnb.lottie.LottieCompositionFactory;
import com.airbnb.lottie.LottieDrawable;
import com.fun.funlivewallpaper.R;

public class SurfaceView1 extends SurfaceView implements SurfaceHolder.Callback {
    public interface SurfaceInterface {
        boolean isPreview();
    }


    public static class MyHandlerThread extends HandlerThread implements ValueAnimator.AnimatorUpdateListener {
        private Handler mHandler;
        private final Runnable mRunnable;
        private ValueAnimator valueAnimator;
        private ValueAnimator valueAnimator2;

        public MyHandlerThread(Runnable runnable0) {
            super("SurfaceView1_DrawThread");
            this.mRunnable = runnable0;
        }

        public void cancelAnimator() {
            Handler handler0 = this.mHandler;
            if (handler0 != null) {
                handler0.removeCallbacksAndMessages(null);
                this.mHandler = null;
            }

            ValueAnimator valueAnimator0 = this.valueAnimator2;
            if (valueAnimator0 != null) {
                valueAnimator0.removeAllUpdateListeners();
                this.valueAnimator2.cancel();
                this.valueAnimator2 = null;
            }

            ValueAnimator valueAnimator1 = this.valueAnimator;
            if (valueAnimator1 != null) {
                valueAnimator1.removeAllUpdateListeners();
                this.valueAnimator.cancel();
                this.valueAnimator = null;
            }
        }

        public void startAnimator(Runnable runnable0) {
            if (this.valueAnimator == null) {
                this.valueAnimator = ValueAnimator.ofFloat(new float[]{0.0f, 1.0f});
            }

            if (this.mHandler == null) {
                this.mHandler = new Handler(this.getLooper());
            }

            this.mHandler.removeCallbacksAndMessages(null);
            this.mHandler.post(new Runnable() {
                @Override
                public void run() {
                    try {
                        runnable0.run();
                    } catch (Exception exception0) {
                        exception0.printStackTrace();
                    }
                }
            });
        }

        private void startValueAnimator() {
            if (this.mRunnable != null && this.valueAnimator2 == null) {
                ValueAnimator valueAnimator0 = ValueAnimator.ofInt(new int[]{0, 1});
                this.valueAnimator2 = valueAnimator0;
                valueAnimator0.setRepeatCount(-1);
                this.valueAnimator2.addUpdateListener(this);
                this.valueAnimator2.start();
            }
        }

        public ValueAnimator getValueAnimator() {
            return this.valueAnimator;
        }

        @Override  // android.animation.ValueAnimator$AnimatorUpdateListener
        public void onAnimationUpdate(ValueAnimator valueAnimator0) {
            this.startAnimator(this.mRunnable);
        }

        @Override
        public void start() {
            synchronized (this) {
                super.start();
                this.startValueAnimator();
            }
        }
    }

    private SurfaceHolder mSurfaceHolder;
    private SurfaceInterface sInterface;
    private static int px20;
    private static int px25;
    private LottieDrawable live1_ball_lottie;
    private LottieDrawable live1_finger_lottie;
    private Bitmap backgroundBitmap;
    private Bitmap maskingGuide1Bitmap;
    private LottieDrawable lottieDrawable3;
    private RectF mRectF;
    private int screenWidth;
    private int halfScreenWidth;
    private int screenHeight;
    private MyHandlerThread mHandlerThread;
    private boolean isSurfacecreated;

    private Bitmap userDefaultWallpaperBitmap;

    private InterpolatorDurationHandle interpolatorDurationHandle;
    private InterpolatorDurationHandle lottieBallInterpolatorDurationHandle;

    private float lottiePositionTopY;

    private long lastLottieSetProgressTime;
    private long dTime;

    private final Runnable aRunnable;
    private boolean isVisible;

    static {
        SurfaceView1.px20 = ScreenHelper.getDimensionPixelSize(R.dimen.gz_dp_20);
        SurfaceView1.px25 = SizeUtils.dp2px(25.0f);
    }

    public void setSufaceHolder(SurfaceHolder surfaceHolder0, SurfaceInterface surfaceView1$IIILiLlllL0) {
        this.mSurfaceHolder = surfaceHolder0;
        this.sInterface = surfaceView1$IIILiLlllL0;
        surfaceHolder0.addCallback(this);
    }

    public SurfaceView1(Context context0) {
        this(context0, null);
    }

    public SurfaceView1(Context context0, AttributeSet attributeSet0) {
        this(context0, attributeSet0, -1);
    }

    public SurfaceView1(Context context0, AttributeSet attributeSet0, int v) {
        this(context0, attributeSet0, v, -1);
    }

    public SurfaceView1(Context context0, AttributeSet attributeSet0, int v, int v1) {
        super(context0, attributeSet0, v, v1);
        this.dTime = 0L;
        this.lastLottieSetProgressTime = 0L;

        this.aRunnable = () -> {
            Canvas canvas0;
            SurfaceHolder surfaceHolder0;
            try {
                surfaceHolder0 = this.getHolder();
                canvas0 = surfaceHolder0.lockCanvas();
                try {
                    label_19:
                    this.drawableBackground(canvas0);
                    this.drawableMask1(canvas0);
                    this.drawLottieFingler(canvas0);
                    this.drawLottieBall(canvas0);
                }
                catch(Exception exception0) {
                    exception0.printStackTrace();
                }
                surfaceHolder0.unlockCanvasAndPost(canvas0);
            }
            catch(Exception exception0) {
                surfaceHolder0 = null;
                canvas0 = null;
            }
            catch(Throwable throwable0) {
                surfaceHolder0 = null;
                canvas0 = null;
            }
        };

        this.backgroundBitmap = WallpaperBitmapHelper.getInstance().getWallpaperBackgroundBitmap();
        this.maskingGuide1Bitmap = WallpaperBitmapHelper.getInstance().getMaskingGuide1Bitmap();

        isSurfacecreated = false;
        this.mRectF = new RectF();
        int v2 = ScreenUtils.getScreenWidth();
        this.screenWidth = v2;
        this.halfScreenWidth = v2 / 2;
        this.screenHeight = ScreenUtils.getScreenHeight();
        try {
            SurfaceHolder surfaceHolder0 = super.getHolder();
            this.mSurfaceHolder = surfaceHolder0;
            surfaceHolder0.addCallback(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.mRectF.set(0.0f, 0.0f, ((float) this.screenWidth), ((float) this.screenHeight));
    }

    @Override
    public SurfaceHolder getHolder() {
        SurfaceHolder surfaceHolder0 = this.mSurfaceHolder;
        return surfaceHolder0 == null ? super.getHolder() : surfaceHolder0;
    }

    public void onVisibilityChanged(boolean z) {
        this.isVisible = z;
//        RedPocketWindowAndBallSurfaceInter.getInstance().onVisibilityChanged(z);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {

        }
        return true;
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder holder) {

        if (this.sInterface != null && !this.sInterface.isPreview()) {
            LottieDrawable lottieFinger = this.live1_finger_lottie;
            if (lottieFinger != null) {
                lottieFinger.removeAllUpdateListeners();
                this.live1_finger_lottie.cancelAnimation();
            }

            LottieDrawable lottieDrawable1 = this.live1_ball_lottie;
            if (lottieDrawable1 != null) {
                lottieDrawable1.removeAllUpdateListeners();
                this.live1_ball_lottie.cancelAnimation();
            }

            LottieDrawable lottieDrawable2 = this.lottieDrawable3;
            if (lottieDrawable2 != null) {
                lottieDrawable2.removeAllUpdateListeners();
                this.lottieDrawable3.cancelAnimation();
            }

            MyHandlerThread mHt = this.mHandlerThread;
            if (mHt != null) {
                mHt.cancelAnimator();
                this.mHandlerThread.quitSafely();
                this.mHandlerThread = null;
            }

            this.cancelAll();
            return;
        }
        this.isSurfacecreated = true;
        MyHandlerThread surfaceView1$lIlI1liIIL1 = this.mHandlerThread;
        if (surfaceView1$lIlI1liIIL1 != null) {
            surfaceView1$lIlI1liIIL1.cancelAnimator();
            this.mHandlerThread.quitSafely();
            this.mHandlerThread = null;
        }

        MyHandlerThread surfaceView1$lIlI1liIIL2 = new MyHandlerThread(null);
        this.mHandlerThread = surfaceView1$lIlI1liIIL2;
        surfaceView1$lIlI1liIIL2.start();
        this.mHandlerThread.startAnimator(this::initLottieAndDraw);


    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
        synchronized(this) {
            if(this.sInterface == null || (this.sInterface.isPreview())) {
                this.isSurfacecreated = false;
            }

            MyHandlerThread surfaceView1$lIlI1liIIL0 = this.mHandlerThread;
            if(surfaceView1$lIlI1liIIL0 != null) {
                surfaceView1$lIlI1liIIL0.cancelAnimator();
                this.mHandlerThread.quitSafely();
                this.mHandlerThread = null;
            }

            LottieDrawable lottieDrawable0 = this.live1_finger_lottie;
            if(lottieDrawable0 != null) {
                lottieDrawable0.removeAllAnimatorListeners();
                this.live1_finger_lottie.cancelAnimation();
                this.live1_finger_lottie = null;
            }

            LottieDrawable lottieDrawable1 = this.live1_ball_lottie;
            if(lottieDrawable1 != null) {
                lottieDrawable1.removeAllAnimatorListeners();
                this.live1_ball_lottie.cancelAnimation();
                this.live1_ball_lottie = null;
            }

            LottieDrawable lottieDrawable2 = this.lottieDrawable3;
            if(lottieDrawable2 != null) {
                lottieDrawable2.removeAllAnimatorListeners();
                this.lottieDrawable3.cancelAnimation();
                this.lottieDrawable3 = null;
            }

            return;
        }
    }


    private void initLottieAndDraw() {
        this.initLottieBall();
        this.initLottiePostionTopY();
        this.drawCanvas();
        this.valueAnimatorStart(this.mHandlerThread.getValueAnimator());
    }

    private void valueAnimatorStart(ValueAnimator valueAnimator0) {
        if(valueAnimator0 != null) {
            valueAnimator0.setRepeatCount(-1);

            valueAnimator0.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    runOnUiThread(valueAnimator);
                }
            });
            new String("固真_计步_悬浮球");
            StringBuilder stringBuilder2 = new StringBuilder();
            stringBuilder2.append("预览壁纸动画回调，drawThread==null:");
            stringBuilder2.append(this.mHandlerThread == null);
            if(!valueAnimator0.isStarted()) {
                new String("固真_计步_悬浮球");
                new String("globalAnimator.start()");
            }

            if(valueAnimator0.isRunning()) {
                valueAnimator0.cancel();
            }

            valueAnimator0.start();
            Log.i("sssssssss", "4");
        }
    }

    private void runOnUiThread(ValueAnimator valueAnimator0) {
        ThreadUtils.runOnUiThread(this::setLottieProgress);
    }

    private void setLottieProgress() {
        this.dTime = this.lastLottieSetProgressTime == 0L ? 0L : System.currentTimeMillis() - this.lastLottieSetProgressTime;
        LottieDrawable lottieDrawable0 = this.live1_finger_lottie;
        if(lottieDrawable0 != null) {
            InterpolatorDurationHandle idh = this.interpolatorDurationHandle;
            if(idh != null) {
                float progress = idh.L1lLIll1lI1(this.dTime).floatValue();
                lottieDrawable0.setProgress(progress);
            }
        }

        LottieDrawable lottieDrawable1 = this.live1_ball_lottie;
        if(lottieDrawable1 != null) {
            InterpolatorDurationHandle i1IILLII1 = this.lottieBallInterpolatorDurationHandle;
            if(i1IILLII1 != null) {
                lottieDrawable1.setProgress(i1IILLII1.L1lLIll1lI1(this.dTime).floatValue());
            }
        }
        new String("固真_计步_悬浮球");
        StringBuilder stringBuilder0 = new StringBuilder();
        stringBuilder0.append("预览壁纸动画回调，drawThread==null:");
        stringBuilder0.append(this.mHandlerThread == null);
        if(this.mHandlerThread != null) {
            new String("固真_计步_悬浮球");
            new String("预览壁纸动画回调线程刷新");
            this.mHandlerThread.startAnimator(this.aRunnable);
        }

        this.lastLottieSetProgressTime = System.currentTimeMillis();
    }

    private void initLottieBall() {
        LottieDrawable lottieDrawable0 = new LottieDrawable();
        this.live1_ball_lottie = lottieDrawable0;
        lottieDrawable0.enableMergePathsForKitKatAndAbove(true);
        this.live1_ball_lottie.setComposition(((LottieCompositionFactory.fromAssetSync(this.getContext(), "lottie/live1_wallpaper/live1_ball.lottie").getValue())));
        this.live1_ball_lottie.setRepeatCount(-1);
        this.live1_ball_lottie.setBounds(0, 0, ScreenHelper.getDimensionPixelSize(R.dimen.gz_dp_360), ScreenHelper.getDimensionPixelSize(R.dimen.gz_dp_720));
        this.lottieBallInterpolatorDurationHandle = new InterpolatorDurationHandle(((long) this.live1_ball_lottie.getComposition().getDuration()));
    }

    private void initLottiePostionTopY() {
        LottieDrawable lottieDrawable0 = new LottieDrawable();
        this.live1_finger_lottie = lottieDrawable0;
        lottieDrawable0.enableMergePathsForKitKatAndAbove(true);
        LottieComposition lottieCompositin = (LottieComposition)LottieCompositionFactory.fromAssetSync(this.getContext(), "lottie/live1_wallpaper/live1_finger.lottie").getValue();
        this.live1_finger_lottie.setComposition(lottieCompositin);
        this.live1_finger_lottie.setRepeatCount(-1);
        boolean isNavBarVisible = false;
        this.live1_finger_lottie.setBounds(0, 0, ScreenHelper.getDimensionPixelSize(R.dimen.gz_dp_170), ScreenHelper.getDimensionPixelSize(R.dimen.gz_dp_176));
        this.interpolatorDurationHandle = new InterpolatorDurationHandle(((long)this.live1_finger_lottie.getComposition().getDuration()));
        DeviceChecker devicerChecker = DeviceChecker.instance;
        if(devicerChecker.isSpecialDevices()) {
            SurfaceView1.px25 = SizeUtils.dp2px(10.0f);
        }
        else if(devicerChecker.isHuawei()) {
            SurfaceView1.px25 = SizeUtils.dp2px(30.0f);
        }
        else {
            SurfaceView1.px25 = SizeUtils.dp2px(55.0f);
        }


        this.lottiePositionTopY = (float)(this.screenHeight - this.live1_finger_lottie.getBounds().height() - SurfaceView1.px25);
    }


    private void drawCanvas() {
        Canvas canvas0;
        SurfaceHolder surfaceHolder0;
        try {
            surfaceHolder0 = this.getHolder();
            canvas0 = surfaceHolder0.lockCanvas();
            try {
                this.drawableBackground(canvas0);
                this.drawableMask1(canvas0);
                this.drawLottieFingler(canvas0);
                this.drawLottieBall(canvas0);
            }
            catch(Exception exception0) {
                exception0.printStackTrace();
            }
            surfaceHolder0.unlockCanvasAndPost(canvas0);
        }
        catch(Exception exception0) {
            surfaceHolder0 = null;
            canvas0 = null;
        }
        catch(Throwable throwable0) {
            surfaceHolder0 = null;
            canvas0 = null;
        }
    }
    private void drawableBackground(Canvas canvas0) {
        if(this.backgroundBitmap == null) {
            this.backgroundBitmap = WallpaperBitmapHelper.getInstance().getWallpaperBackgroundBitmap();
        }

        Bitmap bitmap0 = this.backgroundBitmap;
        if(bitmap0 != null && canvas0 != null) {
            canvas0.drawBitmap(bitmap0, 0.0f, 0.0f, null);
        }
    }
    private void drawableMask1(Canvas canvas0) {
        if(this.maskingGuide1Bitmap == null) {
            this.maskingGuide1Bitmap = WallpaperBitmapHelper.getInstance().getMaskingGuide1Bitmap();
        }

        Bitmap bitmap0 = this.maskingGuide1Bitmap;
        if(bitmap0 != null && canvas0 != null) {
            canvas0.drawBitmap(bitmap0, 0.0f, ((float)(this.screenHeight - bitmap0.getHeight())), null);
        }
    }

    private void drawLottieFingler(Canvas canvas0) {
        if(this.live1_finger_lottie != null && canvas0 != null) {
            canvas0.save();
            canvas0.translate(((float)(this.halfScreenWidth + SurfaceView1.px20)), this.lottiePositionTopY);
//            canvas0.translate(0, 0);
            this.live1_finger_lottie.draw(canvas0);

            canvas0.restore();
        }
    }

    private void drawLottieBall(Canvas canvas0) {
        if(this.live1_ball_lottie != null) {
            canvas0.save();
            canvas0.translate(0.0f, 0.0f);
            this.live1_ball_lottie.draw(canvas0);
            canvas0.restore();
        }
    }

    public void cancelAll() {
        MyHandlerThread mHt = this.mHandlerThread;
        if (mHt != null) {
            mHt.cancelAnimator();
            this.mHandlerThread.quitSafely();
            this.mHandlerThread = null;
        }

//        i1LLi11IL1.LilIiL().whenWallpaperCancel(this, this.screenWidth, this.screenHeight);
//        if(i1LLi11IL1.LilIiL().LL1IL11i11l()) {
//            MyHandlerThread mTd = new MyHandlerThread(() -> this.isVisibleDrawDefault());
//            this.mHandlerThread = mTd;
//            mTd.start();
//            return;
//        }
//
        this.drawUserOrgDefaultWallpaperAndOverlaysBall();
    }


    private void drawUserOrgDefaultWallpaperAndOverlaysBall() {
        Canvas canvas0;
        SurfaceHolder surfaceHolder0;
        try {
            surfaceHolder0 = this.getHolder();
            canvas0 = surfaceHolder0.lockCanvas();
            try {
                this.drawUserOrgDefaultWallpaper(canvas0);
                this.drawOverlaysBall(canvas0);
            } catch (Exception unused_ex) {
            }
            surfaceHolder0.unlockCanvasAndPost(canvas0);

        } catch (Exception unused_ex) {
            surfaceHolder0 = null;
            canvas0 = null;
        } catch (Throwable throwable0) {
            surfaceHolder0 = null;
            canvas0 = null;
        }


    }


    @SuppressLint("MissingPermission")
    private void drawUserOrgDefaultWallpaper(Canvas canvas0) {
        Bitmap bitmap0 = WallpaperBitmapHelper.getInstance().getUserDefautWallpaper();
        this.userDefaultWallpaperBitmap = bitmap0;
        if (bitmap0 == null) {
            WallpaperManager wallpaperManager0 = WallpaperManager.getInstance(this.getContext());
            try {
                this.userDefaultWallpaperBitmap = ((BitmapDrawable) wallpaperManager0.getDrawable()).getBitmap();
                RectF rectF0 = new RectF(0.0f, 0.0f, ((float) this.screenWidth), ((float) this.screenHeight));
                canvas0.drawBitmap(this.userDefaultWallpaperBitmap, null, rectF0, null);
            } catch (Exception exception0) {
                exception0.printStackTrace();
            }

            return;
        }

        RectF rectF1 = new RectF(0.0f, 0.0f, ((float) this.screenWidth), ((float) this.screenHeight));
        canvas0.drawBitmap(this.userDefaultWallpaperBitmap, null, rectF1, null);
    }

    private void drawOverlaysBall(Canvas canvas0) {
    }
}